<?php
namespace Agent\Controller;
use Think\Controller;
//外部接口
class ApiController extends Controller {
    public function _initialize(){
		//数据库连接基础配置-begin
		C('db_config.DB_TYPE',C('DB_TYPE'));
		C('db_config.DB_HOST',C('DB_HOST'));
		C('db_config.DB_USER',C('DB_USER'));
		C('db_config.DB_PWD',C('DB_PWD'));
		C('db_config.DB_PORT',C('DB_PORT'));
		C('db_config.DB_CHARSET',C('DB_CHARSET'));
		//数据库连接基础配置-end
    }
	
	//支付确认
	public function alipay_confirm(){

		$data=file_get_contents("php://input");
		file_put_contents('./log/alipay_confirm.log',date('Y-m-d H:i:s').$data.PHP_EOL,FILE_APPEND);//日志导出sql
		$param=json_decode($data,true);
		if(!isset($data) || count($data)==0){
            $return['status']=0;
			$return['message']='没有数据';
			$this->ajaxReturn($return);
        }
		$pay_no=$param['pay_no'];
		// $pay_no="OD000007044";//测试支付单号
		if(!$pay_no){
			alert_json(0,"没有支付单号");
		}
		$db_name=C('DB_PREFIX_T').'distribution';
		C('db_config.DB_NAME',$db_name);//分销商数据库

		$m=M('Checkbill',C('DB_PREFIX_T'), C('db_config'));
		$p=M('Pushorder',C('DB_PREFIX_T'), C('db_config'));
		
		$map['pay_no']=$pay_no;
		$m_info=$m->where($map)->find();
		$ids=$m_info['ids'];
		if(empty($m_info)){
			alert_json(0,"没有找到对应单号");
		}else{
			if($m_info['pay_status']==1){
				alert_json(0,"已经支付过了");
			}
			$res_sss=$this->owner_pushorder($pay_no,"$ids");//同步订单到货主erp
			file_put_contents('./log/alipay_confirm_ress_sss.log',date('Y-m-d H:i:s').$data.PHP_EOL,FILE_APPEND);//日志导出sql
			$map_p['id']=array('in',"$ids");
			$data_status['pay_status']=1;
			$res_m=$m->where($map)->save($data_status);
			$res_p=$p->where($map_p)->save($data_status);
			alert_json(1,"支付成功");
		}
	}
	//同步订单到货主erp
	public function owner_pushorder($pay_no,$ids){
		$db_name=C('DB_PREFIX_T').'distribution';
		C('db_config.DB_NAME',$db_name);//分销商数据库
		$p=M('Pushorder', C('DB_PREFIX_T'), C('db_config'));
		$cb=M('Checkbill', C('DB_PREFIX_T'), C('db_config'));
		$pg=M('Pushordergoods', C('DB_PREFIX_T'), C('db_config'));
		$map_p['id']=array('in',"$ids");
		$p_list=$p->where($map_p)->select();
		
		$cb_list=$cb->where(array('pay_no'=>$pay_no))->find();
		foreach($p_list as $ks=>$vs){
			$p_list[$ks]['goods']=$pg->where(array('order_id'=>$vs['id']))->select();
		}
		
		// $push_data['erp_code']='48';//测试erp_code
		$push_data['erp_code']='37';//erp_code
		$push_data['order_list']=$p_list;
		$push_data['checkbill_list']=$cb_list;

		// $api_url="http://erpvr.jm.com/index.php/Company/Api/agent_pushorder";//测试 账册商品信息接口
		$api_url="http://www.t-cang.com/erpts/index.php/Company/Api/agent_pushorder";//账册商品信息接口
		if(empty($api_url)){
			alert_json(0,'请配置冻结库存接口');
		}
		$res_pp=push($api_url,$push_data);
		// var_dump($res_pp);
		// die;
		$res_pp=json_decode($res_pp,true);
		file_put_contents('./log/owner_pushorder.log',date('Y-m-d H:i:s').var_export($res_pp,true).PHP_EOL,FILE_APPEND);//日志导出sql
		if($res_pp['status']){
			return 1;
		}else{
			return 0;
		}
	}


	//订单状态接口
	public function order_status(){
		$db_name=C('DB_PREFIX_T').'distribution';
		C('db_config.DB_NAME',$db_name);//分销商数据库
		$data=file_get_contents("php://input");
		file_put_contents('./log/order_status2.log',date('Y-m-d H:i:s').$data.PHP_EOL,FILE_APPEND);//日志导出sql
		// $data='{"apply_order_no":"C20171207140997","logistics_no":"120265637865","is_pass":"1","is_delivery":"1","fail_reason":null,"actual_weight":"1.0020","delivery_time":"1512638491","order_no":"P2017120700005","agent_id":"19"}';
        $param=json_decode($data,true);
		if(!isset($data) || count($data)==0){
            $return['status']=0;
			$return['order_no']='';
			$this->ajaxReturn($return);
        }
		
		$map['order_no']=$param['order_no'];
		$map['agent_id']=$param['agent_id'];
		
		$data_pushorder=array();
		$data_pushorder['apply_order_no']=$param['apply_order_no'];
		$data_pushorder['logistics_no']=$param['logistics_no'];
		$data_pushorder['is_pass']=$param['is_pass'];
		$data_pushorder['is_delivery']=$param['is_delivery'];
		$data_pushorder['fail_reason']=$param['fail_reason'];

		$m=M('Pushorder', C('DB_PREFIX_T'), C('db_config'));
		$res=$m->where($map)->save($data_pushorder);

		$return['order_no']=$param['order_no'];
		if($res===false){
			$return['status']=0;
		}else{
			$return['status']=1;
		}
		
		$this->ajaxReturn($return);
	}

}